﻿using Repid.Entity.Basic;
using Repid.MvcExtension.Controllers;
using Repid.Core.Aop;
using Repid.Utility.Helper;
using Repid.MvcExtension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Repid.MvcApplication.Areas.Manage.Controllers
{
    public class UserController : ManageController
    {
        public ActionResult List(string _menuid)
        {
            return View(ServiceFactory.BasicMenu.Get(_menuid));
        }

        public ActionResult Page(string id)
        {
            BasicUser model;

            if (string.IsNullOrEmpty(id))
            {
                model = new BasicUser();

                model.Head = "null.png";
                model.Tag = string.Empty;
                model.IsSuper = false;
                model.IsEnabled = true;
            }
            else { model = ServiceFactory.BasicUser.Get(id); }

            return View(model);
        }

        public ActionResult UpdatePwd()
        {
            return View();
        }

        public ActionResult ResetPwd(string id)
        {
            BasicUser model;

            if (string.IsNullOrEmpty(id))
            {
                model = new BasicUser();
            }
            else { model = ServiceFactory.BasicUser.Get(id); }

            return View(model);
        }

        public string Grid(int rows, int page, string search, string sort, string order)
        {
            int count = 0;

            var list = ServiceFactory.BasicUser.Grid(rows, page, search, sort, order, out count);

            return JsonHelper.Grid(list, count, ignore: new string[] { "PassWord" });
        }

        public string Save(BasicUser model)
        {
            return ServiceFactory.BasicUser.Save(model).Serialize();
        }

        public string Remove(string id)
        {
            return ServiceFactory.BasicUser.Delete(id).Serialize();
        }

        public string SetResetPwd(BasicUser model)
        {
            return ServiceFactory.BasicUser.ResetPwd(model, SessionFactory.UserName).Serialize();
        }

        public string SetUpdatePwd(string id, string oldpwd, string newpwd)
        {
            return ServiceFactory.BasicUser.UpdatePwd(id, oldpwd, newpwd).Serialize();
        }

        public string EnableOrDisable(string id, bool bit)
        {
            return ServiceFactory.BasicUser.EnableOrDisable(id, bit).Serialize();
        }
    }
}
